﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
namespace RTS_WebServer
{
    class Client
    {
        /// <summary>
        /// Procceses this instance.
        /// </summary>
       public static void Procces(TcpClient Child){
            // Our Stream with Request or DDosers we never know
            NetworkStream Flood = Child.GetStream();
            //Lets Start our Quest here!
            RTSRequest Quest = new RTSRequest();
            //Do we need to mirror everything?
            bool Mirror = false;
            //Is there still data?
            byte[] Buffer = new Byte[4096];
            while (Flood.DataAvailable)
            {
                Flood.Read(Buffer,0,Buffer.Length);
                int i = 0;
                while (i < Buffer.Length)
                {
                    if (Mirror) Flood.WriteByte(Buffer[i]);
                    else if
                    (!Quest.ProccesChar((char)Buffer[i])) Mirror = true; ;
                    i++;
                }


            }
            //string path =  (Program.Configuration["path"] + Quest.Path).Split(new char[]{'?'},2)[0];
            //string resp = "HTTP/1.1 203 LANG VERHAAL\r\nDate: Mon, 19 Jul 2004 16:18:20 GMT\r\nServer: RTS Beta\r\nLast-Modified: Sat, 10 Jul 2004 17:29:19 GMT\r\nAccept-Ranges: bytes\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n";
            
            //byte[] Recipe = Encoding.ASCII.GetBytes(resp);
            //Flood.Write(Recipe,0,Recipe.Length);
            RTSResponse rep = new RTSResponse(Quest);
            rep.Answer(Flood);
            Child.Client.Shutdown(SocketShutdown.Both);
            Child.Close();
        }
     static  X509Certificate Cert = new X509Certificate("localhost.pfx", "500Please");
            public    static void Procces(TcpClient Child,System.Net.Security.SslStream Flood,NetworkStream FatherStream){
                try
                {
                    Flood.AuthenticateAsServer(Cert);
                }
                catch (Exception ex)
                {
                    return;
                }
            //Lets Start our Quest here!
            RTSRequest Quest = new RTSRequest();
            //Do we need to mirror everything?
            bool Mirror = false;
            //Is there still data?
          //  byte[] strange = new byte[4];
          //  Flood.Read(strange,0,4);
           // System.Threading.Thread.Sleep(500);
            byte[] Buffer = new Byte[4096];          
            while (FatherStream.DataAvailable)
            {
                Flood.Read(Buffer, 0, Buffer.Length);
                int i = 0;
                while (i < Buffer.Length)
                {
                    if (Mirror) Flood.WriteByte(Buffer[i]);
                    else if
                    (!Quest.ProccesChar((char)Buffer[i])) Mirror = true; ;
                    i++;
                }
            }
            //string path =  (Program.Configuration["path"] + Quest.Path).Split(new char[]{'?'},2)[0];
            //string resp = "HTTP/1.1 203 LANG VERHAAL\r\nDate: Mon, 19 Jul 2004 16:18:20 GMT\r\nServer: RTS Beta\r\nLast-Modified: Sat, 10 Jul 2004 17:29:19 GMT\r\nAccept-Ranges: bytes\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n";
            
            //byte[] Recipe = Encoding.ASCII.GetBytes(resp);
            //Flood.Write(Recipe,0,Recipe.Length);
            if (!Mirror)
            {
                RTSResponse rep = new RTSResponse(Quest);
                rep.Answer(Flood);
                
            }
            Child.Client.Shutdown(SocketShutdown.Both);
            Child.Close();
        }
    }
}
